<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="doClick"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <!--current-page控制当前的页码值  -->
              <!--page-size当前每页显示多少条  -->
              <!--page-sizes 每页显示条数的选项 -->
              <!--total 总条数 -->
              <!--layout 布局组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2,3,5,6,30]"
                :page-size="pageParams.pagesize"
                layout="total,sizes, prev, pager, next,jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <el-dialog
          :title=" isEdit ? '编辑角色' : '新增角色'"
          :visible.sync="showDialog"
          @click="hClose"
        >
          <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small">取消</el-button>
              <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
            </el-col>
          </el-row>
          <el-dialog />
        </el-dialog></el-card>
    </div>
  </div>
</template>
<script>
import { getRoles, addRoles, editRoles, delRoles } from '@/api/setting'

export default {
  data() {
    return {
      list: [],
      pageParams: {
        page: 1, // 页码值
        pagesize: 2// 每页显示的条数

      },
      total: 0,
      showDialog: false,
      isEdit: false,
      rules: {
        name: [{ required: true, message: '输入角色名称', trigger: 'blur' }],
        description: [{ required: true, message: '输入角色描述', trigger: 'blur' }]
      },
      roleForm: {
        name: '',
        description: ''
      }

    }
  },
  created() {
    this.loadRoles()
    // ·
  },
  methods: {
    hClose() {
      // 重置表单
      // resetFields将表单回到初始状态（清空表单检验规则）
      this.$refs.roleForm.resetFields()
      // 手动清空
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    // 删除
    hDel(id) {
      this.$confirm('您确认删除这个角色么').then(async() => {
        const res = await delRoles(id)
        console.log(res, 'delRoles')
        if (this.list.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        if (res.code === 10000) {
          this.loadRoles()
        }
      }).catch(error => console.log(error))
    },
    // 发送请求 获取数据列表
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams)
        console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        this.$message.error('获取失败')
      }
    },
    // 当每页显示的条数进行切换时触发---val:每页显示的条数
    handleSizeChange(val) {
      console.log(`每页${val}条数`)
      this.pageParams.page = 1
      this.pageParams.pagesize = val
      // 重新发送请求获取当前页面所对应的数据
      this.loadRoles()
    },
    // 当前的页码值进行切换时触发---val:当前的页码值
    handleCurrentChange(val) {
      console.log(`当前页:${val}`)
      this.pageParams.page = val
      // 重新发送请求获取当前页面所对应的数据
      this.loadRoles()
    },
    // 控制表单显示隐藏
    doClick() {
      this.isEdit = false
      this.showDialog = true
    },
    hSubmit() {
      this.$refs.roleForm.validate(valid => {
        if (!valid) return // 校验失败
        // 校验通过,实现新增或编辑
        this.isEdit ? this.doEdit : this.doAdd
      })
    },
    // 编辑角色 获取表单数据 填充到表单
    hEdit(row) {
      console.log(row)
      // 将传递过来的角色详情填充
      this.roleForm = row
      this.roleForm = { ...row }
      this.isEdit = true
      this.showDialog = true
    },
    async doEdit() {
      try {
        const res = await editRoles(this.roleForm)
        console.log(res, 'editRoles')
        // 新增完成 将总数加1
        this.total++
        // 计算出最大的页码值 Math.ceil(总的条数 / 每页显示的条数)
        const maxPage = Math.ceil(this.total / this.pageParams.pagesize)
        // 将请求参数的page改成最大页码值
        this.pageParams.page = maxPage
        // 重新获取数据
        this.loadRoles()
        // 关闭弹层
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 实现新增数据
    async   doAdd() {
      try {
        const res = await addRoles(this.roleForm)
        console.log(res)
        // 新增完成 将总数加1
        this.total++
        // 计算出最大的页码值 Math.ceil(总的条数 / 每页显示的条数)
        const maxPage = Math.ceil(this.total / this.pageParams.pagesize)
        // 将请求参数的page改成最大页码值
        this.pageParams.page = maxPage
        // 重新获取数据
        this.loadRoles()
        // 关闭弹层
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
